//
//  LYAccountAlertPolicyView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/28.
//

import UIKit

import SnapKit

class LYAccountAlertPolicyView: UIView {
    
    let fullString = LYlocalizedString("agree_above")
    let tapString = ""
    // MARK: - Properties
    var onAgreementTapped: (() -> Void)? // 点击协议的回调
    
    var isSelected: Bool {
        return radioButton.isSelected
    }
    
    // MARK: - UI Components
    
    private lazy var radioButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "privacy_normal"), for: .normal)
        btn.setImage(UIImage(named: "privacy_selected"), for: .selected)
        btn.addTarget(self, action: #selector(radioButtonTapped(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var agreementLabel: UILabel = {
        let label = UILabel()
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleAgreementTap)))
        
        // 富文本设置（核心修改部分）
        let attributedString = NSMutableAttributedString(string: fullString)
        let range = (attributedString.string as NSString).range(of: tapString)

        // 非点击部分的普通样式
        let fullRange = NSRange(location: 0, length: attributedString.length)
        attributedString.addAttributes([
            .font: UIFont.systemFont(ofSize: 12),
            .foregroundColor: UIColor(hex: "#000000") // 非点击部分颜色
        ], range: fullRange)
        
        attributedString.addAttributes([
            .foregroundColor: UIColor(hex: "#000000"),
            .underlineStyle: NSUnderlineStyle.single.rawValue,
            .font: UIFont.boldSystemFont(ofSize: 12) // 加粗效果
        ], range: range)

        label.attributedText = attributedString
        return label
    }()
    
    // MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup (SnapKit 核心部分)
    private func setupUI() {
        // 添加子视图
        addSubview(radioButton)
        addSubview(agreementLabel)
        
        // SnapKit 约束配置
        
        radioButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        agreementLabel.snp.makeConstraints { make in
            make.left.equalTo(radioButton.snp.right).offset(8)
            make.right.lessThanOrEqualToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
    }
    
    // MARK: - Actions
    @objc private func radioButtonTapped(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
    @objc private func handleAgreementTap() {
        onAgreementTapped?()
    }
}
